package com.serverTechnology.craft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.serverTechnology.craft.entity.dto.StepsResourceInsertDto;
import com.serverTechnology.craft.entity.vo.StandInfoVo;
import com.serverTechnology.craft.entity.vo.StandardVo;
import com.serverTechnology.craft.mapper.StepsResourceTypeMapper;
import com.serverTechnology.craft.entity.StepsResourceType;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serverTechnology.craft.service.StepsResourceTypeService;
import com.serverTechnology.resource.entity.DeviceEntity;
import com.serverTechnology.resource.entity.ManagementEntity;
import com.serverTechnology.resource.mapper.DeviceMapper;
import com.serverTechnology.resource.mapper.ManagementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 工步所需资源类型 服务实现类
 * </p>
 *
 * @author 王烁
 * @since 2024-08-01
 */
@Service
public class TeStepsResourceTypeServiceImpl extends ServiceImpl<StepsResourceTypeMapper, StepsResourceType> implements StepsResourceTypeService {
    //管理设备、工装、工具类型
    @Autowired
    private DeviceMapper deviceMapper;
    //工步所需资源类型
    @Autowired
    private StepsResourceTypeMapper stepsResourceTypeMapper;
    //管理设备、工装、工具
    @Autowired
    private ManagementMapper managementMapper;

    /**
     * 查询是否为非标设备 0是标准 1不是
     *
     * @param standard 根据是否为标准
     * @return List<StandardVo> id和名称
     */
    @Override
    public List<StandardVo> isNonStandard(Long standard) {
        List<StandardVo> standardVos = new ArrayList<>();
        LambdaQueryWrapper<DeviceEntity> managementEntityMPJLambdaWrapper = new LambdaQueryWrapper<>();
        managementEntityMPJLambdaWrapper.select(DeviceEntity::getId, DeviceEntity::getDeviceName);
        managementEntityMPJLambdaWrapper.eq(DeviceEntity::getDeviceState, standard);
        managementEntityMPJLambdaWrapper.eq(DeviceEntity::getDeviceDistinguish, "EQUIPMENT");
        List<DeviceEntity> deviceEntities = deviceMapper.selectList(managementEntityMPJLambdaWrapper);
        deviceEntities.forEach(deviceEntity -> {
            StandardVo standardVo = new StandardVo();
            standardVo.setId(deviceEntity.getId());
            standardVo.setResourceName(deviceEntity.getDeviceName());
            standardVos.add(standardVo);
        });
        return standardVos;
    }

    /**
     * 工装、工具所需要的下拉框
     *
     * @param deviceDistinguish 设备EQUIPMENT，工具TOOL，工装FROCK
     * @return 设备、工装、工具名称和id
     */
    @Override
    public List<StandardVo> getStepsDeviceDistinguish(String deviceDistinguish) {
        List<StandardVo> standardVos = new ArrayList<>();
        LambdaQueryWrapper<DeviceEntity> deviceEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deviceEntityLambdaQueryWrapper.eq(DeviceEntity::getDeviceDistinguish, deviceDistinguish);
        List<DeviceEntity> deviceEntities = deviceMapper.selectList(deviceEntityLambdaQueryWrapper);
        deviceEntities.forEach(deviceEntity -> {
            StandardVo standardVo = new StandardVo();
            standardVo.setId(deviceEntity.getId());
            standardVo.setResourceName(deviceEntity.getDeviceName());
            standardVos.add(standardVo);
        });
        return standardVos;
    }
    /***
     *  添加所需标准、非标设备、工装、工具
     * @param stepsResourceInsertDto 标准、非标设备、工装、工具
     * @return
     */
    @Override
    public void insertStepsResource(StepsResourceInsertDto stepsResourceInsertDto) {
        StepsResourceType stepsResourceType = new StepsResourceType();
        stepsResourceType.setProcessStepsId(stepsResourceInsertDto.getProcessStepsId());
        stepsResourceType.setDeviceDistinguish(stepsResourceInsertDto.getDeviceDistinguish());
        stepsResourceType.setDeviceId(stepsResourceInsertDto.getDeviceId());
        stepsResourceTypeMapper.insert(stepsResourceType);
    }
    /***
     * 删除所需标准、非标设备、工装、工具
     * @param id
     * @return
     */
    @Override
    public void deleteStepsResource(Long id) {
        stepsResourceTypeMapper.deleteById(id);
    }
    /**
     * 查询工装、工具
     * @param id 工序id
     * @return List<StandInfoVo>
     */
    @Override
    public List<StandInfoVo> getStepsResource(Long id) {
       MPJLambdaWrapper<StepsResourceType> stepsResourceTypeMPJLambdaWrapper = new MPJLambdaWrapper<>();
       stepsResourceTypeMPJLambdaWrapper.select(StepsResourceType::getId, StepsResourceType::getDeviceId, StepsResourceType::getDeviceDistinguish);
       stepsResourceTypeMPJLambdaWrapper.select(DeviceEntity::getDeviceName);
        stepsResourceTypeMPJLambdaWrapper.eq(StepsResourceType::getProcessStepsId, id);
        stepsResourceTypeMPJLambdaWrapper.ne(DeviceEntity::getDeviceDistinguish, "EQUIPMENT");
        stepsResourceTypeMPJLambdaWrapper.leftJoin(DeviceEntity.class, DeviceEntity::getId, StepsResourceType::getDeviceId);
        List<StandInfoVo> standInfoVos = stepsResourceTypeMapper.selectJoinList(StandInfoVo.class, stepsResourceTypeMPJLambdaWrapper);
        return standInfoVos;
    }

    @Override
    public List<StandInfoVo> getStepsEquipment(Long id) {
        MPJLambdaWrapper<StepsResourceType> stepsResourceTypeMPJLambdaWrapper = new MPJLambdaWrapper<>();
        stepsResourceTypeMPJLambdaWrapper.select(StepsResourceType::getId, StepsResourceType::getDeviceId, StepsResourceType::getDeviceDistinguish);
        stepsResourceTypeMPJLambdaWrapper.select(DeviceEntity::getDeviceName, DeviceEntity::getDeviceState);
        stepsResourceTypeMPJLambdaWrapper.eq(StepsResourceType::getProcessStepsId, id);
        stepsResourceTypeMPJLambdaWrapper.eq(DeviceEntity::getDeviceDistinguish, "EQUIPMENT");
        stepsResourceTypeMPJLambdaWrapper.leftJoin(DeviceEntity.class, DeviceEntity::getId, StepsResourceType::getDeviceId);
        List<StandInfoVo> standInfoVos = stepsResourceTypeMapper.selectJoinList(StandInfoVo.class, stepsResourceTypeMPJLambdaWrapper);
        return standInfoVos;
    }
}
